package com.bjsubway.gov.info.manager.dao;

import com.bjsubway.gov.info.manager.domain.Subway;

import java.io.*;
import java.util.ArrayList;

public class SubwayDao {

     private static ArrayList<Subway> subList = new ArrayList<>();

    public  void check() {
        try {
            BufferedReader br = new BufferedReader(new FileReader("bjsubway-gov-info-manager\\subways.txt"));
            String line;
            while ((line = br.readLine()) != null) {
                String[] split = line.split(",");
                System.out.println(split[0] + "\t\t" + split[1] + "\t\t" + split[2]);
                Subway s = new Subway(split[0],split[1],split[2]);
                subList.add(s);
            }
            br.close();
        } catch (IOException e) {
        }
        if(subList.isEmpty()){
            System.out.println("暂无数据");
        }
    }

    public void add(String num, String name, String line) {
        Subway subway = new Subway(num, name, line);
        subList.add(subway);
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter("bjsubway-gov-info-manager\\subways.txt", true));
            for (Subway sub : subList) {
                bw.write(sub.getSubNum() + "," + sub.getSubName() + "," + sub.getLine());
                bw.newLine();
            }
            bw.close();
        } catch (IOException e) {
        }
    }

}
